import socket
import pymysql

conn = pymysql.connect(host='localhost', user='root', password='lqh270774337', port=3306, db='mysql')
cursor = conn.cursor()

BUFSIZE = 1024
ip_port = ('192.168.3.83', 8666)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # udp协议
server.bind(ip_port)
while True:
    data,client_addr = server.recvfrom(BUFSIZE)
    data = data.decode()
    data = data.strip().split('|')
    print('server收到的数据', data)
    print(float(data[0]))
    h = float(data[0])
    t = float(data[1])
    light = int(data[2])
    cursor.execute("insert into mysql.polls_humidity_temperature(humidity,temperature,light) values('%f','%f','%d')" %(h,t,light))
        # 提交到数据库执行
    conn.commit()

    #server.sendto(data.upper(),client_addr)

cursor.close()
conn.close()
server.close()
